package com.example.liuxing.jdmall.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.example.liuxing.jdmall.JDApplication;
import com.example.liuxing.jdmall.MainActivity;
import com.example.liuxing.jdmall.R;
import com.example.liuxing.jdmall.bean.RLoginResult;
import com.example.liuxing.jdmall.bean.RResult;
import com.example.liuxing.jdmall.constant.IdiyMessage;
import com.example.liuxing.jdmall.controller.LoginController;
import com.example.liuxing.jdmall.db.UserDao;
import com.example.liuxing.jdmall.listener.IModeChangeListener;
import com.example.liuxing.jdmall.utils.ActivityUtils;

/**
 * Created by liuxing on 2017/7/13.
 */

public class LoginActivity extends BaseActivity implements IModeChangeListener { // 登录

    private EditText name_et; // 账号
    private EditText pwd_et;  // 密码
    private LoginController loginVc;

    private Handler mhandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case IdiyMessage.LOGIN_ACTION_RESULT:
                    handleLoginResult(msg);
                    break;
                case IdiyMessage.SAVE_USERTODB_RESULT:
                    handleSave2Db((boolean)msg.obj);
                    break;
                case IdiyMessage.READ_USERTODB_ACTION_RESULT:
                    handleUserInfo(msg.obj);
                    break;
            }
        }
    };

    private void handleUserInfo(Object object) { // 读取user
        if (object != null) {
            UserDao.UserInfo userInfo = (UserDao.UserInfo)object;
            name_et.setText(userInfo.name);
            pwd_et.setText(userInfo.pwd);
        }
    }

    private void handleSave2Db(boolean ifSuccess) {
        if (ifSuccess) {
            ActivityUtils.start(this, MainActivity.class);
        } else {
            tipMessage("登录异常");
        }
    }

    // 登录的数据
    private void handleLoginResult(Message msg) {
        String name = name_et.getText().toString();
        String pwd = pwd_et.getText().toString();
        RResult result = (RResult)msg.obj;
        if (result.isSuccess()) {
            // 登录成功
//            ActivityUtils.start(this, MainActivity.class);
            // 保存数据
            loginVc.sendAsyncMessage(IdiyMessage.SAVE_USERTODB, name, pwd);
            // 将用户信息保存到Application
            RLoginResult bean = JSON.parseObject(result.getResult(), RLoginResult.class);
            ((JDApplication)getApplication()).setRLoginResult(bean);
        } else {
            //登录失败
            tipMessage("登录失败" + result.getErrorMsg());
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        name_et = (EditText)findViewById(R.id.name_et);
        pwd_et = (EditText)findViewById(R.id.pwd_et);
        loginVc = new LoginController(this);
        // 设置接口
        loginVc.setIModeChangeListener(this);

        // 如果数据库有数据，直接读取
        loginVc.sendAsyncMessage(IdiyMessage.READ_USERTODB_ACTION, 0);
    }

    public void loginClick(View view) { // 登录按钮点击
        String name = name_et.getText().toString();
        String pwd = pwd_et.getText().toString();

        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
            tipMessage("请输入账号密码");
            return;
        }
        // 发送网络请求
        loginVc.sendAsyncMessage(IdiyMessage.LOGIN_ACTION, name, pwd);
    }

    @Override
    public void onModelChanged(int action, Object... valuves) {
        mhandler.obtainMessage(action, valuves[0]).sendToTarget();
    }
}
